package com.gxuwz.medical.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
/**
 * 农民档案信息查询DAO类
 * @author 演示
 *
 */
public class PersonDao extends GenericDao<Person> {
	
	@Override
	protected Person handle(ResultSet rs) throws SQLException {
		//创建农民档案实例
		Person entity=new Person();
		entity.setPerscode(rs.getString("perscode"));
		//关联家庭档案信息
		entity.setFamily(new Family(rs.getString("famicode")));
		entity.setRuralCard(rs.getString("ruralCard"));
		entity.setRelation(rs.getString("relation"));
		entity.setCardID(rs.getString("cardID"));
		entity.setPersname(rs.getString("persname"));
		entity.setAge(rs.getInt("age"));
		entity.setSex(rs.getString("sex"));
		entity.setBirthday(rs.getString("birthday"));
		entity.setPersPro(rs.getString("persPro"));
		entity.setRural(rs.getBoolean("isRural"));
		entity.setOccupation(rs.getString("occupation"));
		entity.setWorkUnit(rs.getString("workUnit"));
		entity.setLiveAddress(rs.getString("liveAddress"));
		entity.setTelephone(rs.getString("telephone"));
		return entity;
	}
	/**
	 * 根据姓名模糊查询个人记录，考虑同名的情况
	 * @param name
	 * @param areacode
	 * @return
	 * @throws SQLException
	 */
	public List<Person> findByKeyWord(String name,String areacode)throws Exception{
		try{
			String sql="select * from t_person where famicode like'"
		             +areacode+"%' and persname like'%"+name+"%'";
		    Object[]params=null;
			List<Person> persons=super.queryOjects(sql, params);
			return persons;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}
	/**
	 * 根据家庭编号+年度查找未缴费人员列表
	 * @param famicode
	 * @param payYear
	 * @return
	 * @throws Exception
	 */
	public List<Person>findUnpayList(String famicode,int payYear)throws Exception{
		try{
			String sql="select * from t_person where famicode=? and "
					+ "perscode not in"
					+ "(select perscode from t_payment where famicode=? and runyear=?)";
		    Object[]params={famicode,famicode,payYear};
			List<Person> persons=super.queryOjects(sql, params);
			return persons;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}
	/**
	 * 根据名字查找户主信息
	 * @param name
	 * @param areacode
	 * @return
	 * @throws Exception
	 */
	public List<Person> findHostByName(String name,String areacode)throws Exception{
		try{
			String sql="select * from t_person where relation=1 and famicode like'"
		             +areacode+"%' and persname like'%"+name+"%'";
		    Object[]params=null;
			List<Person> persons=super.queryOjects(sql, params);
			return persons;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}
}
